//
//  XJSeckillGoodsCollectionViewCell.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/10.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSeckillGoodsCollectionViewCell.h"

#import "SeckillGoodsModel.h"

#import "UIImage+ColorImage.h"
#import <UIImageView+WebCache.h>
#import "UILabel+CountDown.h"

@interface XJSeckillGoodsCollectionViewCell ()

@property (nonatomic, strong) UIImageView * skGoodsImgV;//商品图片
@property (nonatomic, strong) UILabel * skGoodsTitleLab;//商品名称
@property (nonatomic, strong) UILabel * skGoodsPriceLab;//商品价格
@property (nonatomic, strong) UILabel * skGoodsDiscardLab;//弃用价格

@property (nonatomic, strong) UILabel * countDownLab;//倒计时


@property (nonatomic, strong) NSIndexPath *tempIndexPath;


@end

@implementation XJSeckillGoodsCollectionViewCell

- (UIImageView *)skGoodsImgV{
    if (!_skGoodsImgV) {
        UIImageView * skGoodsImgV = [UIImageView new];
        self.skGoodsImgV = skGoodsImgV;
        [self addSubview:skGoodsImgV];
    }
    return _skGoodsImgV;
}

- (UILabel *)skGoodsTitleLab{
    if (!_skGoodsTitleLab) {
        UILabel * skGoodsTitleLab = [UILabel new];
        self.skGoodsTitleLab = skGoodsTitleLab;
        [self addSubview:skGoodsTitleLab];
    }
    return _skGoodsTitleLab;
}

- (UILabel *)skGoodsPriceLab{
    if (!_skGoodsPriceLab) {
        UILabel * skGoodsPriceLab = [UILabel new];
        self.skGoodsPriceLab = skGoodsPriceLab;
        [self addSubview:skGoodsPriceLab];
    }
    return _skGoodsPriceLab;
}

- (UILabel *)skGoodsDiscardLab{
    if (!_skGoodsDiscardLab) {
        UILabel * skGoodsDiscardLab = [UILabel new];
        self.skGoodsDiscardLab = skGoodsDiscardLab;
        [self addSubview:skGoodsDiscardLab];
    }
    return _skGoodsDiscardLab;
}

- (UILabel *)countDownLab{
    if (!_countDownLab) {
        UILabel * countDownLab = [UILabel new];
        self.countDownLab = countDownLab;
    }
    return _countDownLab;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupSubviews];
        
        //接收通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:kSecKillGoodsCollectionViewCellNotification object:nil];
    }
    return self;
}

- (void)setupSubviews{
    
    self.skGoodsImgV.frame = CGRectMake(ADAPTWidth(4), 0, (KWidth - 4 )/2, (KWidth - 4 )/2);
    [self.skGoodsImgV sd_setImageWithURL:[NSURL URLWithString:@"http://img.suneasecloud.com/uploadfilesapi/435/201801/22/201801220005.jpg"] placeholderImage:[UIImage imageWithColor:XJCOLOR_RGB(221, 221, 221)]];
    self.skGoodsImgV.contentMode =  UIViewContentModeScaleAspectFill;
    self.skGoodsImgV.layer.masksToBounds = YES;
    self.skGoodsImgV.layer.cornerRadius = 10.0;
    self.skGoodsImgV.sd_layout
    .centerXEqualToView(self)
    .topEqualToView(self)
    .widthIs(ADAPTWidth(175))
    .heightIs(ADAPTHeight(175));
    
    NSString * countDownTitle = @"  03:22:52后结束";
    [self.countDownLab setupCountDownTitle:countDownTitle height:30];
    [self.skGoodsImgV addSubview:self.countDownLab];
    self.countDownLab.sd_layout
    .leftSpaceToView(self.skGoodsImgV, 15)
    .rightSpaceToView(self.skGoodsImgV, 15)
    .bottomSpaceToView(self.skGoodsImgV, 10)
    .heightIs(30);
    
    self.skGoodsTitleLab.font = XJFONTSET_R(14);
    self.skGoodsTitleLab.textColor = XJCOLOR_RGB(17, 17, 17);
    self.skGoodsTitleLab.numberOfLines = 2;
    self.skGoodsTitleLab.text = @"商品名商品名商品名商品名商品名商品名商品名商品名";
    self.skGoodsTitleLab.sd_layout
    .leftSpaceToView(self,ADAPTHeight(8))
    .topSpaceToView(self.skGoodsImgV, 4)
    .widthIs(ADAPTWidth(175))
    .heightIs(50);

    self.skGoodsPriceLab.font = XJFONTSET_M(16);
    self.skGoodsPriceLab.textColor = XJCOLOR_RGB(244, 67, 54);
    self.skGoodsPriceLab.text = @"￥99.00";
    CGFloat skGoodsPriceLabW = [UILabel labelWithLabel:self.skGoodsPriceLab getWidthWithTitle:self.skGoodsPriceLab.text font:self.skGoodsPriceLab.font];
    CGFloat skGoodsPriceLabH = [UILabel labelWithLabel:self.skGoodsPriceLab getHeightByWidth:skGoodsPriceLabW title:self.skGoodsPriceLab.text font:self.skGoodsPriceLab.font];
    self.skGoodsPriceLab.sd_layout
    .leftSpaceToView(self, ADAPTHeight(8))
    .topSpaceToView(self.skGoodsTitleLab, 1.5)
    .widthIs(skGoodsPriceLabW)
    .heightIs(skGoodsPriceLabH);
    
    self.skGoodsDiscardLab.font = XJFONTSET_R(14);
    self.skGoodsDiscardLab.textColor = XJCOLOR_RGB(121, 130, 133);
    NSString * priceString = @"￥199.00";

    CGFloat skGoodsDiscardLabW = [UILabel labelWithLabel:self.skGoodsDiscardLab getWidthWithTitle:priceString font:self.skGoodsDiscardLab.font];
    CGFloat skGoodsDiscardLabH = [UILabel labelWithLabel:self.skGoodsDiscardLab getHeightByWidth:skGoodsDiscardLabW title:priceString font:self.skGoodsDiscardLab.font];
    self.skGoodsDiscardLab.sd_layout
    .rightSpaceToView(self,ADAPTHeight(8))
    .topSpaceToView(self.skGoodsTitleLab, 2)
    .widthIs(skGoodsDiscardLabW)
    .heightIs(skGoodsDiscardLabH);
    
    //添加中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.skGoodsDiscardLab.text attributes:attribtDic];
    // 赋值
    self.skGoodsDiscardLab.attributedText = attribtStr;
}

- (void)setSgModel:(SeckillGoodsModel *)sgModel{
    
    _sgModel = sgModel;
    
    UIImage *placeholderImg = [UIImage imageNamed:@"default_goods"];
    
    // 赋值
    [_skGoodsImgV sd_setImageWithURL:[NSURL URLWithString:sgModel.cover] placeholderImage:placeholderImg];
    
    _skGoodsTitleLab.text  = sgModel.name;
    
    _skGoodsPriceLab.text = [NSString stringWithFormat:@"%.2f",[sgModel.price floatValue]];
    
    NSString *marketPrice = [NSString stringWithFormat:@"%.2f",[sgModel.market_price floatValue]];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:marketPrice attributes:attribtDic];
    // 赋值
    self.skGoodsDiscardLab.attributedText = attribtStr;

    
//    _countDownLab.text =
}


#pragma mark - 给cell赋值
- (void)configureSecKillGoodsClvCellWithSecKillModel:(SeckillGoodsModel *)sgModel indexPath:(NSIndexPath *)indexPath {
    
    _sgModel = sgModel;
    _tempIndexPath = indexPath;
    
    
    UIImage *placeholderImg = [UIImage imageNamed:@"default_goods"];
    
    // 赋值
    [_skGoodsImgV sd_setImageWithURL:[NSURL URLWithString:sgModel.cover] placeholderImage:placeholderImg];
    
    _skGoodsTitleLab.text  = sgModel.name;
    
    _skGoodsPriceLab.text = [NSString stringWithFormat:@"%.2f",[sgModel.price floatValue]];
    
    NSString *marketPrice = [NSString stringWithFormat:@"%.2f",[sgModel.market_price floatValue]];
    
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:marketPrice attributes:attribtDic];
    // 赋值
    self.skGoodsDiscardLab.attributedText = attribtStr;

    
    NSString *countDownTitle = [sgModel currentTimeString];
    
    [self.countDownLab setupCountDownTitle:countDownTitle height:30];
}

#pragma mark - 接收通知事件
- (void)notificationAction:(NSNotification *)notification {
    
    [self configureSecKillGoodsClvCellWithSecKillModel:_sgModel indexPath:_tempIndexPath];
}




@end
